package com.pclong.commons;

import com.pclong.commons.http.*;
import com.pclong.config.FileListener;
import com.pclong.config.FileMonitor;
import com.pclong.config.FileObserver;
import com.pclong.config.entity.CardTypeConfigEntity;
import com.pclong.config.entity.ServerConfigEntity;
import com.pclong.config.entity.TankAgentConfigEntity;
import com.pclong.model.TblBean;
import com.pclong.sql.DataSourceProvider;
import com.pclong.sql.SQLHelper;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.sql.*;
import java.util.Random;
import org.sqlite.JDBC;
import com.pclong.util.debug.Debug;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws IOException, Exception {
        String result = null;
        /* 测试post方法*/
        /*
        HttpWebClient httpweb=new HttpWebClient();
        Map<String, Object> params=new HashMap<String, Object>();
        System.out.println( httpweb.post("http://www.baidu.com/",params) );
         * */
//         HttpWebClient httpweb=new HttpWebClient();  
//         String response=httpweb.getJson("http://assist1.gn.zing.vn/");
//        System.out.println(response);

//        读写控制台
//        while (true) {
//            String str = readString("请输入字符串：");
//            if (str.endsWith("exit")) {
//                break;
//            }
//            System.out.println("readString 方法的输入：" + str);
//        }
//        result = HttpHelper.hexDigest("abc");
//        System.out.println(result);


        //File file = new File("E://temp//1//ServerConfigEntity.xml");         

//        ServerConfigEntity rule = new ServerConfigEntity();
//        rule.Id=1;
//        Serializer serializer = new Persister();
//        //序列化  
//        serializer.write(rule, file);
//        //反序列化  
//        rule = serializer.read(ServerConfigEntity.class, file);
//        System.out.println(TankAgentConfigEntity.getInstance().server.Id);
//        //ServerConfigEntity.getInstance().Id=0;
//        TankAgentConfigEntity.getInstance().server=rule;
         System.out.println(TankAgentConfigEntity.getInstance().getFilePath());


        //监听文件夹   
//        FileObserver ob = new FileObserver("E:\\temp\\1");  
//        FileListener listener = new FileListener();  
//        ob.addListener(listener);  
//        FileMonitor monitor = new FileMonitor(ob);  
//        monitor.start();  
//        
//         while (true) {
//            String str = readString("请输入字符串：");
//            if (str.endsWith("exit")) {
//                monitor.stop();
//                break;
//            }            
//        }

        /*
        long startTime = System.nanoTime();  //開始時間
        for(int i = 0;i<10000;i++){
        ;
        }
        long consumingTime = System.nanoTime() - startTime; //消耗時間
        System.out.println(consumingTime);
        System.out.println(consumingTime/1000+"微秒");
         */

        //SqlLite();
        //TestSql() ;
        //TestLogs();
    }

    private static String readString(String prompt) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        try {
            System.out.print(prompt);
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static void SqlLite() throws Exception {
        try {
            //连接SQLite的JDBC

            Class.forName("org.sqlite.JDBC");

            //建立一个数据库名zieckey.db的连接，如果不存在就在当前目录下创建之

            Connection conn = DriverManager.getConnection("jdbc:sqlite:zieckey.db");

            Statement stat = conn.createStatement();

//            stat.executeUpdate("create table tbl1(name varchar(20), salary int);");//创建一个表，两列
//
//
//            stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);"); //插入数据
//
//            stat.executeUpdate("insert into tbl1 values('LiSi',7800);");
//            stat.executeUpdate("insert into tbl1 values('WangWu',5800);");
//            stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);");

            ResultSet rs = stat.executeQuery("select * from tbl1;"); //查询数据

            while (rs.next()) { //将查询到的数据打印出来

                System.out.print("name = " + rs.getString("name") + " "); //列属性一

                System.out.println("salary = " + rs.getString("salary")); //列属性二

            }
            rs.close();
            conn.close(); //结束数据库的连接

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void TestSql() {
        SQLHelper sqlh = new SQLHelper();
//       sqlh.insertRecord("tbl1", new String[]{"name"},new TblBean());
        int rows = sqlh.insertRecord("tbl1", new String[]{"name"}, new Object[]{"long"});
        System.out.println(rows);
    }

    public static void TestLogs() {
        Debug debug = DataSourceProvider.getDebug(App.class);
        debug.error("error");
        //debug.debug("debug");
    }
}
